import sys
import os, django, time
import hashlib
import requests
from random import Random

sys.path.append("..")
sys.path.append("../utils")
os.environ.update({"DJANGO_SETTINGS_MODULE": "server.settings"})
django.setup()
from django.test import Client
from user.models import Company, WarZone, User
from storehouse.models import Storehouse
from django.db import transaction
from user.models import FileStore
from software.utils import fileStore

from conf.sysConfig import config

qa = os.path.join(config["dataDir"], config["questionAnswer"])

client = Client()

baseUrl = u"http://127.0.0.1:8000/api/user/"


# baseUrl = u"http://192.168.10.3:8000/api/user/"

def register():
    iputData = {
        "username": "user2",
        "name": "user2",
        "password": "123456",
        "phone": "1111112",
        "warZoneId": "5",
        "companyId": "5",
        "userType": "3",
    }
    res = requests.post(baseUrl + 'register/', data=iputData)
    print(res.content)


def login():
    iputData = {
        'username': 'uuuu',
        'password': '123456',
        'systemType': '1',
    }
    res = requests.post(baseUrl + 'login/', data=iputData)
    # pdb.set_trace()
    print(res.content)


def logout():
    iputData = {
        'token': '24af9e53417b3d766552',
    }
    res = requests.post(baseUrl + 'logout/', data=iputData)
    # pdb.set_trace()
    print(res.content)


def get_valid_img():
    iputData = {

    }
    res = requests.post(baseUrl + 'getCode/', data=iputData)
    # pdb.set_trace()
    print(res.content)


def isOnline():
    iputData = {
        'token': '61aa3da28253d3877c20',
    }
    res = requests.post(baseUrl + 'isOnline/', data=iputData)
    # pdb.set_trace()
    print(res.content)


def getLocalToken():
    timeStamp = time.time()
    localTime = time.localtime(timeStamp)
    strTime = time.strftime("%Y%m%d%H", localTime)
    str = randomNumber(30)
    hl = hashlib.md5()
    hl.update(str.encode(encoding='utf-8'))
    return hl.hexdigest()[0:20]


def randomNumber(length=20):
    string = ''
    chars = '0123456789'
    random = Random()
    for i in range(length):
        string += chars[random.randint(0, len(chars) - 1)]
    return string


def searchUser():
    inputData = {
        'userName': 'admin',
    }
    res = client.post(baseUrl + 'searchUser/', inputData, HTTP_X_REQUESTED_WITH='XMLHttpRequest')
    print(res.content)
    # res = requests.post(baseUrl + 'searchUser/', data=inputData)
    # pdb.set_trace()


@transaction.atomic()
def create_war_com():
    wa = WarZone.objects.create(
        name="基础战区",
        warzoneState="1",
    )
    con = Company.objects.create(
        name="基础单位",
        warZone=wa,
        companyState="1",
    )
    user = User.objects.create(
        company=con,
        warZone=wa,
        username="develop",
        name="开发人员",
        userId="123123123123123",
        userType="1",
        password="123456",
    )
    user2 = User.objects.create(
        company=con,
        warZone=wa,
        username="admin",
        name="管理人员",
        userId="1231231231231232",
        userType="2",
        password="123456",
        isSupervisor=True
    )
    user3 = User.objects.create(
        company=con,
        warZone=wa,
        username="user",
        name="使用人员",
        userId="1231231231231233",
        userType="3",
        password="123456"
    )
    sh = Storehouse.objects.create(
        storehouseName=u'本地仓库',
        storehouseState=0,
        storeIPType=1,
        connectState=0,
        runState=0,
        company=con,
        warZone=wa,
        isMasterStore=True
    )
    fileStore.saveToDatabase(fileName='QA.xlsx', user=user2, filePath=os.path.join(qa, 'QA.xlsx'),
                             fileType='6', uniqueFile=getLocalToken())
    sh.user.add(user)
    sh.user.add(user2)
    sh.save()


def create_user():
    pass
    print(123123123)


if __name__ == '__main__':
    # register()
    # login()
    # logout()
    # get_valid_img()
    # create_war_com()
    # create_user()
    # isOnline()
    searchUser()
